﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PhuLeDao.Research;
using PhuLeBussinessObject.Research;
using PhuLeBooze.Global;
using System.Web.Script.Serialization;
using PhuLeBussinessObject.Core;
using System.Web.SessionState;
using System.Xml;
using System.Text;
using PhuLeDao.Core;

namespace PhuLeBooze.Views.Accounts
{
    public partial class ExportExcel : System.Web.UI.Page
    {
        GridView GridView1 = new GridView();
        protected void Page_Load(object sender, EventArgs e)
        {
            string Context = "";
            if (Request.QueryString["Completed"].ToString() == "1")
                Context = "  Context=\"PASSCONDITIONS\" ";
            string InputValue = string.Format("<InputValue RowsPerPage=\"{0}\" PageIndex=\"{1}\" {2}/>", 1000000, 1, Context);
            GridView1.DataSource = (new CCoreDao()).GetMemberListExport(InputValue);
            GridView1.DataBind();
            Export();
        }
        protected void Export()
        {
            Response.Clear();
            Response.Buffer = true;
            string filename = "6sao_Member";
            if (Request.QueryString["Type"].ToString() == "1")
                filename = "6sao_Member";
            filename += "_" + DateTime.Now.ToString();
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", filename));


            GridView1.AllowPaging = false;
            GridView1.DataBind();

            //Change the Header Row back to white color
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

            //Apply style to Individual Cells
            GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];

                //Change Color back to white
                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row
                row.Attributes.Add("class", "textmode");

                //Apply style to Individual Cells of Alternating Row
                if (i % 2 != 0)
                {
                    row.Cells[0].Style.Add("background-color", "#C2D69B");
                    row.Cells[1].Style.Add("background-color", "#C2D69B");
                    row.Cells[2].Style.Add("background-color", "#C2D69B");
                    row.Cells[3].Style.Add("background-color", "#C2D69B");
                }
            }
            // GridView1.RenderControl(hw);



            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

            GridView1.RenderControl(hw);

            Response.Write(sw.ToString());
            Response.Flush();
            Response.End();



        }
    }
}